#include "../mqserver/message.hpp"

#include <gtest/gtest.h>

mymq::MessageManager::ptr mmp;

class MessageTest: public testing::Environment
{
    virtual void SetUp() override
    {
        mmp = std::make_shared<mymq::MessageManager>("./data/message");
        mmp->InitQueueMessage("queue1");
    }
    virtual void TearDown() override
    {
        // mmp->Clear();
        std::cout << "完成资源的清理化工作" << std::endl;
    }
};

// TEST(message_test, insert_test)
// {
//     mymq::BasicProperties property;
//     property.set_id(mymq::UUIDHelper::Getuuid());
//     property.set_delivery_mode(mymq::DeliveryMode::DURABLE);
//     property.set_routing_key("new.music.pop");
//     mmp->Insert("queue1", &property, "Hello world-1", mymq::DeliveryMode::DURABLE);
//     mmp->Insert("queue1", nullptr, "Hello world-2", mymq::DeliveryMode::UNDURABLE);
//     mmp->Insert("queue1", nullptr, "Hello world-3", mymq::DeliveryMode::DURABLE);
//     mmp->Insert("queue1", nullptr, "Hello world-4", mymq::DeliveryMode::DURABLE);
//     ASSERT_EQ(mmp->TotalCount("queue1"), 3);
//     ASSERT_EQ(mmp->DurableCount("queue1"), 3);
//     ASSERT_EQ(mmp->GetableCount("queue1"), 4);
//     ASSERT_EQ(mmp->WaitackCount("queue1"), 0);
// }

// TEST(message_test, select_test)
// {
//     mymq::MessagePtr msg = mmp->Front("queue1");
//     ASSERT_EQ(msg->payload().body(), "Hello world-1");
//     ASSERT_EQ(mmp->GetableCount("queue1"), 2);
//     ASSERT_EQ(mmp->WaitackCount("queue1"), 1);
//     msg = mmp->Front("queue1");
//     ASSERT_EQ(msg->payload().body(), "Hello world-3");
//     ASSERT_EQ(mmp->GetableCount("queue1"), 1);
//     ASSERT_EQ(mmp->WaitackCount("queue1"), 2);
//     msg = mmp->Front("queue1");
//     ASSERT_EQ(msg->payload().body(), "Hello world-4");
//     ASSERT_EQ(mmp->GetableCount("queue1"), 0);
//     ASSERT_EQ(mmp->WaitackCount("queue1"), 3);
// }

TEST(message_test, ack_test)
{
    mymq::MessagePtr msg = mmp->Front("queue1");
    ASSERT_EQ(msg->payload().body(), "Hello world-1");
    ASSERT_EQ(mmp->GetableCount("queue1"), 2);
    ASSERT_EQ(mmp->WaitackCount("queue1"), 1);
    mmp->Ack("queue1", msg->payload().properties().id());
    ASSERT_EQ(mmp->GetableCount("queue1"), 2);
    ASSERT_EQ(mmp->DurableCount("queue1"), 2);
    ASSERT_EQ(mmp->TotalCount("queue1"), 3);
}


int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new MessageTest);
    RUN_ALL_TESTS();

    return 0;
}